<template>
	<!-- 门店商品 -->
	<view class="storeShop-main">
		<view style="background-color: #fff; box-sizing: border-box; padding: 12px; height: 105px">
			<view class="storeShop-header" v-if="storeInfo">
				<view class="storeImage">
					<image :src="imageUrls(storeInfo.image)" class="images"></image>
				</view>
				<view class="storeInfo">
					<view class="title">
						{{ storeInfo.name || '暂无' }}
						<text style="color: #999; font-size: 12px; float: right" v-if="shopQuery.type == 1">距离：{{ (storeInfo.distance / 1000).toFixed(2) }}/km</text>
					</view>
					<view class="option">
						<view class="option-item" style="padding-left: 0px">
							<view class="item-name">开店时间</view>
							<view class="item-value">{{ storeInfo.business_start || '00:00' }} - {{ storeInfo.business_end || '00:00' }}</view>
						</view>
						<view class="option-item" style="border-left: 1px #f3f3f3 solid; border-right: 1px #f3f3f3 solid">
							<view class="item-name">起送</view>
							<view class="item-value">
								<text v-if="storeInfo.start_price">
									<text style="font-size: 10px">￥</text>
									{{ storeInfo.start_price }}
								</text>
								<text v-else>无起送价</text>
							</view>
						</view>
					</view>
					<view class="couponList" v-if="couponList.length > 0">
						<view class="coupon-item" v-for="(item, index) in couponList" :key="item.id" @click="gotoPath(item.store_id)">
							<text>满{{ item.full }}减{{ item.price }}</text>
							<text style="padding-left: 5px; border-left: 1px #fff solid; margin-left: 5px">去领取</text>
						</view>
						<view style="color: #999; display: flex; font-size: 10px; box-sizing: border-box; padding: 2px 0px" @click="gotoPath(shopQuery.store_id)">
							更多
							<uni-icons type="right" size="14"></uni-icons>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view style="height: 12px; background-color: #f3f3f3"></view>

		<view class="storeShop-content">
			<view class="header">
				<text class="name">店铺商品</text>
				<view class="search">
					<uni-search-bar
						radius="50"
						v-model="shopQuery.keyword"
						style="flex: 1"
						placeholder="请输入商品名称搜索"
						clearButton="always"
						cancelButton="none"
						@confirm="search"
						@cancel="search"
						@clear="clearSearch"
					/>
					<text class="searchPage-title" @click="search">搜索</text>
				</view>
			</view>
			<view class="content">
				<view class="storeShop-content-left" :style="bottomStyle">
					<view class="classify-item" @click="getIsActive(item.id)" v-for="(item, index) in classifyList" :key="item.id" :class="[isActive == item.id ? 'active' : '']">
						{{ item.name }}
					</view>
				</view>
				<view class="storeShop-content-rigth" :style="bottomStyle">
					<scroll-view scroll-y class="scrollSty" @scrolltolower="scrolltolowerFnc">
						<view v-if="shopListData.length > 0">
							<view class="shopItem" v-for="(item, index) in shopListData" :key="item.id">
								<hwxcShopSidewise :shopInfo="item" />
							</view>
						</view>
						<u-empty v-else mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
					</scroll-view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { cloudGoodsList, cloudStoreCategory, cloudStoreInfo } from '../../api/merchant/index';
import { flashdetail, flashcategory } from '../../api/flash/index';
import { imageUrl } from '../../utils/tools';
import hwxcShopSidewise from '../../components/hwxcShopSidewise/hwxcShopSidewise.vue';
import { userCouponList } from '../../api/home/index';
export default {
	data() {
		return {
			isActive: null,
			// 商品参数
			shopQuery: {
				page: 1,
				limit: 10,
				store_id: '',
				keyword: '',
				type: ''
			},
			// 商品数据
			shopListData: [],
			// 商品分类
			classifyList: [],
			swiperListData: [],
			storeInfo: null,
			couponList: []
		};
	},

	components: {
		hwxcShopSidewise
	},

	computed: {
		bottomStyle() {
			const systemInfo = uni.getSystemInfoSync();
			const { safeAreaInsets } = systemInfo;
			const bottomSafeAreaHeight = safeAreaInsets.bottom || 0;
			return `padding-bottom: ${bottomSafeAreaHeight ? bottomSafeAreaHeight + 'px' : '0rpx'}`;
		}
	},

	async onLoad(option) {
		const { store_id, type, lng, lat } = option;
		// 门店ID
		this.shopQuery.store_id = store_id;
		this.shopQuery.type = type;
		if (type == 0) {
			await this.getcloudStoreInfo(store_id);
			await this.getcloudStoreCategory();
		} else if (type == 1) {
			await this.getflashdetail(store_id, lat, lng);
			await this.getflashcategory();
		}
		await this.getUserCouponList(store_id);
		await this.getHomeGoodsList();
	},

	methods: {
		async getIsActive(index) {
			this.shopQuery.page = 1;
			this.isActive = index;
			this.shopListData = [];
			this.getHomeGoodsList();
		},

		// 商品数据
		async getHomeGoodsList() {
			try {
				let queryData = { ...this.shopQuery };
				if (this.isActive) {
					queryData.category_id = this.isActive;
				}
				const resData = await cloudGoodsList(this.removeEmptyParams(queryData));
				let list = resData.data.data || [];
				const shopListData = [...(this.shopListData || []), ...list];
				shopListData.forEach((item) => {
					item.image = imageUrl(item.fileUrl);
				});
				this.shopListData = shopListData;
			} catch (e) {
				throw Error(e);
			}
		},

		// 删除空值
		removeEmptyParams(obj) {
			Object.keys(obj).forEach((key) => {
				if (obj[key] === null || obj[key] === undefined || obj[key] === '') {
					delete obj[key];
				}
			});
			return obj;
		},

		imageUrls(url) {
			return imageUrl(url);
		},

		// 商品分类 - 云店
		async getcloudStoreCategory() {
			try {
				const resData = await cloudStoreCategory();
				this.classifyList = resData.data;
				if (this.classifyList.length > 0) {
					this.isActive = this.classifyList[0].id;
				}
			} catch (e) {
				throw Error(e);
			}
		},

		// 商品分类 - 闪电送
		async getflashcategory() {
			try {
				const resData = await flashcategory();
				this.classifyList = resData.data;
				if (this.classifyList.length > 0) {
					this.isActive = this.classifyList[0].id;
				}
			} catch (e) {
				throw Error(e);
			}
		},

		search() {
			this.shopQuery.page = 1;
			this.shopListData = [];
			this.getHomeGoodsList();
		},

		clearSearch() {
			this.shopQuery.keyword = '';
			this.shopQuery.page = 1;
			this.shopListData = [];
			this.getHomeGoodsList();
		},

		// 闪电送门店信息
		async getflashdetail(id, lat, lng) {
			try {
				const resData = await flashdetail({
					id,
					lat,
					lng
				});
				this.storeInfo = resData.data;
				const store_layout_images = this.storeInfo.store_layout_images;
				const list = store_layout_images.split(',')
				list.forEach((item) => {
					this.swiperListData.push({
						image: imageUrl(item)
					});
				});
			} catch (e) {
				throw Error(e);
			}
		},

		// 云店门店信息
		async getcloudStoreInfo(id) {
			try {
				const resData = await cloudStoreInfo({
					id
				});
				this.storeInfo = resData.data;
			} catch (e) {
				throw Error(e);
			}
		},

		// 获取卡券
		async getUserCouponList(store_id) {
			const resData = await userCouponList({
				store_id
			});
			const list = resData.data;
			if (list.length > 0) {
				this.couponList = list.slice(0, 2);
			}
		},

		gotoPath(store_id) {
			uni.navigateTo({
				url: `/pages/couponCollection/couponCollection?store_id=${store_id}`
			});
		},

		scrolltolowerFnc() {
			this.shopQuery.page++;
			this.getHomeGoodsList();
		}
	}
};
</script>

<style>
page {
	background-color: #fff;
	height: 100%;
}
</style>

<style scoped lang="scss">
.storeShop-main {
	.navigation {
		height: 50px;
		line-height: 50px;
	}
	.storeShop-header {
		display: flex;
		font-size: 26rpx;
		height: 80px;
		.storeImage {
			width: 70px;
			margin-right: 20rpx;
			.images {
				width: 70px;
				height: 70px;
				border-radius: 3px;
			}
		}

		.storeInfo {
			flex: 1;
			height: 100%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.title {
				font-size: 30rpx;
			}

			.option {
				display: flex;
				align-items: center;
				.option-item {
					padding: 0px 30rpx;
					.item-name {
						color: #999;
						font-size: 27rpx;
					}
				}
			}
		}

		.couponList {
			display: flex;
			.coupon-item {
				box-sizing: border-box;
				padding: 2px 8rpx;
				font-size: 9px;
				background-color: #e43927;
				color: #fff;
				border-radius: 2px;
				margin-right: 5px;
			}
		}
	}

	.storeShop-content {
		height: calc(100vh - 117px);
		box-sizing: border-box;
		background-color: #fff;
		font-size: 26rpx;
		.header {
			padding: 0px 20rpx;
			height: 50px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
			border-bottom: 1px #f3f3f3 solid;
			.name {
				width: 100px;
			}
			.search {
				flex: 1;
				display: flex;
				align-items: center;
				.searchPage-title {
					font-size: 26rpx;
				}
			}
		}
		.content {
			display: flex;
			height: calc(100vh - 170px);
			.storeShop-content-left {
				width: 100px;
				background-color: #f3f3f3;
				margin-right: 20rpx;
				overflow-y: scroll;
				box-sizing: border-box;
				.classify-item {
					height: 50px;
					line-height: 50px;
					width: 100%;
					text-align: center;
				}

				.active {
					background-color: #fff;
					color: #e43927;
				}
			}

			.storeShop-content-rigth {
				flex: 1;
				overflow-y: scroll;
				box-sizing: border-box;
				.scrollSty {
					height: 100%;
					.shopItem {
						box-sizing: border-box;
						padding: 20rpx;
						padding-left: 0px;
					}
				}
			}
		}
	}
}
</style>
